package com.travelsky.flash.controller;

import java.util.Locale;
import java.util.ResourceBundle;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.travelsky.flash.api.IProcessWar;
import com.travelsky.flash.constant.ServiceDeployConfigConstant;
import com.travelsky.flash.vo.UserVO;

@Controller  
@RequestMapping("/")  
public class LoginController {
	
	private static ResourceBundle userAccunt = ResourceBundle.getBundle("userAccount",
			Locale.US);
	@Resource
	private IProcessWar processWar;
	
	@InitBinder("user")
	public void initBinder(WebDataBinder binder) {
		binder.setFieldDefaultPrefix("user."); 
	}

    @RequestMapping("/toLogin")  
    public String toLogin(){
    	return "login";  
    }
    @RequestMapping("/login")  
    public String login(@ModelAttribute("user") UserVO user, HttpSession session){
    	if(null != user) {
	    	for (String key : userAccunt.keySet()) {
				if (user.getName().equals(key)
						&& user.getPassword().equals(userAccunt.getObject(key))) {
					session.setAttribute(ServiceDeployConfigConstant.USER_SESSION, user);
		    		return "redirect:/index.do";
				}
			}
    	}
    	return "redirect:/toLogin.do";  
    }
   
}


